package com.kjtpay.design_pattern.abstract_factory;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.kjtpay.design_pattern.R;
import com.kjtpay.design_pattern.adapter.AdapterActivity;

public class AbstractFactoryActivity extends AppCompatActivity {

    public static void launch(Context context) {
        if (null != context) {
            Intent intent = new Intent();
            intent.setClass(context, AbstractFactoryActivity.class);
            if (!(context instanceof Activity)) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            }
            context.startActivity(intent);
        }
    }

    RadioGroup radioGroupDatabase;
    TextView textViewDatabase;
    User user;
    Department department;
    IUser iUser;
    IDepartment iDepartment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_abstract_factory);
        setTitle("抽象工厂模式");

        radioGroupDatabase = findViewById(R.id.radioGroupDataBase);
        textViewDatabase = findViewById(R.id.textViewDatabase);

        user = new User(1, "小菜");
        department = new Department(2, "研发部");
    }

    public void onInsertClick(View view) {
        changeDatabase();
        String message = "";
        message += iUser.insert(user) + "\n";
        message += iDepartment.insert(department);
        textViewDatabase.setText(message);
    }

    public void onReadClick(View view) {
        changeDatabase();
        String message = "";
        message += iUser.getUser(1) + "\n";
        message += iDepartment.getDepartment(2);
        textViewDatabase.setText(message);
    }

    // 根据用户选择切换数据库
    private void changeDatabase() {
        // 根据用户选择切换数据库
        switch (radioGroupDatabase.getCheckedRadioButtonId()) {
            case R.id.radioButtonSqlServer:
                DataAccess.db = "SqlServer";
                break;
            case R.id.radioButtonAccess:
                DataAccess.db = "Access";
                break;
            default:
                // 什么也不做
                break;
        }
        // 创建方法
        iUser = DataAccess.createUser();
        iDepartment = DataAccess.createDepartment();
    }
}